package com.mlz.mlzmall.ui.activity.act.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.mlz.mlzmall.R;
import com.mlz.mlzmall.ui.activity.act.base.BaseActivity;
import com.mlz.mlzmall.ui.adapter.GuideAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述：引导页
 * Created by benny on 2016/5/7.
 * ProjectName：mlzMall.
 */
public class GuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {
    //引导图片资源

    private int[] pics = {R.mipmap.slogan_01, R.mipmap.slogan_02, R.mipmap.slogan_03};
    private List<ImageView> imageList; // viewpager的数据
    private LinearLayout llPointGroup; // 点的组
    private View mSelectPointView; // 选中的点view对象
    private int basicWidth; // 点之间的宽度
    private Button btnStart; // 开始体验按钮
    private Button btnBypass;  // Bypass Button

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.guide_layout);
        initView();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        ViewPager mViewPager = (ViewPager) findViewById(R.id.vp_guide);
        btnStart = (Button) findViewById(R.id.btn_guide_start_experience);
        btnBypass = (Button) findViewById(R.id.btn_bypass);
        llPointGroup = (LinearLayout) findViewById(R.id.ll_guide_point_group);
        mSelectPointView = findViewById(R.id.select_point);

        initData();

        GuideAdapter mAdapter = new GuideAdapter(GuideActivity.this,imageList);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOnPageChangeListener(this);
        btnStart.setOnClickListener(this);
        btnBypass.setOnClickListener(this);

        // view绘制流程: measure -> layout -> draw
        // 监听mSelectPointView控件layout

        // 获得视图树的观察者, 监听全部布局的回调
        mSelectPointView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                // 只执行一次, 把当前的事件从视图树的观察者中移除掉
                mSelectPointView.getViewTreeObserver().removeGlobalOnLayoutListener(this);

                // 取出两个点之间的宽度
                basicWidth = llPointGroup.getChildAt(1).getLeft() - llPointGroup.getChildAt(0).getLeft();
                //System.out.println("点之间的宽度: " + basicWidth);
            }
        });

        // Show bypass button
        btnBypass.setVisibility(View.VISIBLE);
    }

    int width, height;
    int scale;


    /**
     * 初始化数据
     */
    private void initData() {
        // ctrl + 2 松手停顿 L
        imageList = new ArrayList<ImageView>();

        ImageView iv;
        View view;
        LinearLayout.LayoutParams params;
        for (int i = 0; i < pics.length; i++) {
            iv = new ImageView(this);
            iv.setBackgroundResource(pics[i]);
            imageList.add(iv);

            // 根据图片的个数, 每循环一次向LinearLayout中添加一个点
            view = new View(this);
            view.setBackgroundResource(R.drawable.point_select);
            params = new LinearLayout.LayoutParams(10, 10);
            params.rightMargin = 5;
            params.topMargin = 5;
            params.bottomMargin = 5;
            if (i != 0) {
                params.leftMargin = 20;
            } else
                params.leftMargin = 5;
            view.setLayoutParams(params);
            llPointGroup.addView(view);
        }
    }



    /**
     * 当页面正在滚动时
     * position 当前选中的是哪个页面
     * positionOffset 比例
     * positionOffsetPixels 偏移像素
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        //System.out.println("position: " + position + ", positionOffset: " + positionOffset);

        // 20 * (1 + 0.99) = 38
        int leftMargin = (int) (basicWidth * (position + positionOffset));

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mSelectPointView.getLayoutParams();
        params.leftMargin = leftMargin;
        mSelectPointView.setLayoutParams(params);
    }

    /**
     * 当页面被选中
     */
    @Override
    public void onPageSelected(int position) {
        if (position == imageList.size() - 1) {
            btnStart.setVisibility(View.VISIBLE);
        } else {
            btnStart.setVisibility(View.GONE);
        }
    }

    /**
     * 当页面滚动状态改变
     */
    @Override
    public void onPageScrollStateChanged(int state) {
        System.out.println("onPageScrollStateChanged() state: " + state);
    }

    @Override
    public void onClick(View v) {

        // 打开主页面
        startActivity(new Intent(this, MainActivity.class));
        GuideActivity.this.overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
        finish();
    }
}
